/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Utilitaires.PropertiesReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Properties;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author myriamguinvarch
 */
@ManagedBean
@RequestScoped
public class ContentBean {

    public static int LEGENDE = 1;
    public static int LIEU = 2;
    public static int MONUMENT = 3;
    public static int RESTAURANT = 4;
    
    private int nodeId;
    
    private String titre;
    private String descr;
    private String historique;
    
    private UploadedFile photo;
    private UploadedFile audio;
    private String video;
    
    private String rue;
    private String codePostal;
    private String ville;
    private String siteWeb;
    private String tel;
    private long latitude;
    private long longitude;
    
    private Properties properties;
    
    public void save() throws MalformedURLException, IOException, JSONException
    {
        this.properties = PropertiesReader.getProperties();
        
        URL url = new URL("http://"+properties.getProperty("adress")+":"+properties.getProperty("port")+"/content");
        HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
        connexion.setDoOutput(true);
        connexion.setRequestMethod("PUT");
        
        try
        {
            OutputStreamWriter out = new OutputStreamWriter(connexion.getOutputStream());
            String json = this.generateJson();
            out.write(json);
            out.close();
        }
        catch(Exception e)
        {}
        finally
        {
            System.out.println(connexion.getResponseCode());
            System.out.println(connexion.getResponseMessage());
        }
        System.out.println("ContentBean.save()");
    }
    
    /**
     * Generate JSON from content data
     * @return
     * @throws JSONException 
     */
    public String generateJson() throws JSONException
    {
        JSONObject json = new JSONObject();
        
        // Add objects != String
        json.put("node_id", this.nodeId);
        
        if (this.audio != null) {
            json.put("audio", this.audio.getContents());
        }
        
        if (this.latitude != 0) {
            json.put("lat", this.latitude);
        }
        
        if (this.longitude != 0) {
            json.put("long", this.longitude);
        }
        
        if (this.photo != null) {
            json.put("image", this.photo.getContents());
        }
        
        // Add Strings
        json = this.putString(json, "postal_code", this.codePostal);
        json = this.putString(json, "text", this.descr);
        json = this.putString(json, "history", this.historique);
        json = this.putString(json, "street", this.rue);
        json = this.putString(json, "town", this.ville);
        json = this.putString(json, "website", this.siteWeb);
        json = this.putString(json, "phone", this.tel);
        json = this.putString(json, "title", this.titre);
        json = this.putString(json, "movie", this.video);
        
        
        return json.toString();
    }
    
    /**
     * Put string into json object after nullity was tested.
     * @param json
     * @param key
     * @param object
     * @return
     * @throws JSONException 
     */
    private JSONObject putString (JSONObject json, String key, String object) throws JSONException {
        if (object != null) {
            if (!object.equals("")) {
                json.put(key, object);
            } 
        }
        
        return json;
    }
    
    /** GETTERS AND SETTERS **/
    
    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    
    public String getTitre() {
        return titre;
    }

    public void setTitre(String titre) {
        this.titre = titre;
    }

    public String getHistorique() {
        return historique;
    }

    public void setHistorique(String historique) {
        this.historique = historique;
    }

    public UploadedFile getAudio() {
        return audio;
    }

    public void setAudio(UploadedFile audio) {
        this.audio = audio;
    }

    public String getRue() {
        return rue;
    }

    public void setRue(String rue) {
        this.rue = rue;
    }

    public String getCodePostal() {
        return codePostal;
    }

    public void setCodePostal(String codePostal) {
        this.codePostal = codePostal;
    }

    public String getVille() {
        return ville;
    }

    public void setVille(String ville) {
        this.ville = ville;
    }

    public String getSiteWeb() {
        return siteWeb;
    }

    public void setSiteWeb(String siteWeb) {
        this.siteWeb = siteWeb;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    
    public String getDescr() {
        return descr;
    }

    public void setDescr(String descr) {
        this.descr = descr;
    }

    public UploadedFile getPhoto() {
        return photo;
    }

    public void setPhoto(UploadedFile photo) {
        this.photo = photo;
    }

    public String getVideo() {
        return video;
    }

    public void setVideo(String video) {
        this.video = video;
    }

    public long getLatitude() {
        return latitude;
    }

    public void setLatitude(long latitude) {
        this.latitude = latitude;
    }

    public long getLongitude() {
        return longitude;
    }

    public void setLongitude(long longitude) {
        this.longitude = longitude;
    }
    
    public int getNodeId() {
        return this.nodeId;
    }
    
    public void setNodeId(int nodeId) {
        this.nodeId = nodeId;
    }
    /**
     * Creates a new instance of ContentBean
     */
    public ContentBean() {
    }
    
    
}
